package com.fastloan.mgt.application.approve.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.application.approve.dao.ApplicationApproveHistoryMapper;
import com.fastloan.mgt.application.approve.model.ApplicationApproveFile;
import com.fastloan.mgt.application.approve.model.vo.ApplicationApproveHistoryVO;
import com.fastloan.mgt.application.approve.service.ApplicationApproveFileService;
import com.fastloan.mgt.application.approve.service.ApplicationApproveHistoryService;
import com.fastloan.mgt.application.approve.model.ApplicationApproveHistory;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.sys.model.CmuSysFile;
import com.fastloan.safe.mgt.sys.service.CmSysFileService;
import com.kawins.supers.table.manager.DataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static java.util.stream.Collectors.toList;

/**
 * @author admins
 *         createTime 2017/11/13
 */
@Service
public class ApplicationApproveHistoryServiceImpl extends BasicServiceImpl<ApplicationApproveHistory> implements ApplicationApproveHistoryService {
    @Autowired
    private ApplicationApproveHistoryMapper applicationApproveHistoryMapper;

    @Autowired
    private ApplicationApproveFileService applicationApproveFileService;

    @Autowired
    private CmSysFileService cmSysFileService;

    @Override
    public List<ApplicationApproveHistoryVO> queryApproveHistory(String applicationId) {
        List<ApplicationApproveHistoryVO> applicationApproveHistoryVOS
                = applicationApproveHistoryMapper.queryApproveHistory(applicationId);
        return applicationApproveHistoryVOS.stream()
                .peek(applicationApproveHistoryVO -> {
                    ApplicationApproveFile applicationApproveFile = new ApplicationApproveFile();
                    applicationApproveFile.setAppApproveId(applicationApproveHistoryVO.getAppApproveId());
                    List<ApplicationApproveFile> approveFiles = applicationApproveFileService.select(applicationApproveFile);
                    List<CmuSysFile> sysFileList = getCmSysFiles(approveFiles);
                    applicationApproveHistoryVO.setApproveFiles(sysFileList);
                }).collect(toList());
    }

    @Override
    @DataTable(name = "queryApproveHistoryPageList")
    public List<ApplicationApproveHistoryVO> queryApproveHistoryPageList(Map<String, Object> map) {
        map.put("userId", ManagerKit.getUser().getId());
        return applicationApproveHistoryMapper.queryApproveHistoryPageList(map);
    }

    private List<CmuSysFile> getCmSysFiles(List<ApplicationApproveFile> approveFiles) {
        List<CompletableFuture<CmuSysFile>> completableFutureList = approveFiles
                .stream()
                .map(applicationApproveFile ->
                        CompletableFuture.supplyAsync(()
                                -> cmSysFileService.selectByPrimaryKey(applicationApproveFile.getFileId())))
                .collect(toList());
        return completableFutureList.stream().map(CompletableFuture::join).collect(toList());
    }

}
